package com.epoint.mobileoa.personalized.epointcompany.ui;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.model.CodeItemsModel;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.AddNewWorkContentTask;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.InitAddWorkContentPageInfoTask;
import com.epoint.mobileoa.personalized.epointcompany.config.ConfigKey;
import com.epoint.mobileoa.personalized.epointcompany.model.MissionInfoModel;
import com.epoint.mobileoa.personalized.epointcompany.model.ProjectInfoModel;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.ControlsHelp;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;
import com.epoint.mobileoa.tools.controls.tree.NewTree;
import com.epoint.mobileoa.tools.controls.tree.Tree;

/**
 * @author liyc
 * @time 2012-4-13 下午4:33:40
 * @annotation 
 */
public class AddNewWorkContentView extends SuperView {


	private Spinner sp_gzlx,sp_ywlx,sp_wcbl;
	private EditText et_ywlx,et_xmmc,et_ywsj,et_rwmc,et_jbsj,et_ywlr;
	private Button bt_ywlx,bt_xmxz,bt_tjbc,bt_rwgl;
	private int taskid = 1;//1初始化页面数据,2保存数据
	private String[] compeletepercentS = new String[]{"0%","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"};

	private int resumetype = 0;//0刚进入,1业务类型选择恢复

	private List<CodeItemsModel> gzlxlist;
	private CodeItemsModel cim_ywlx = new CodeItemsModel();
	private ProjectInfoModel pim_xmmc;
	private MissionInfoModel mim_rwgl;
	public static String gzrzguid = "";
	public String rzdate;
	private CheckBox cbIsBm;

	@Override
	public void initControls() {
		sp_gzlx = (Spinner) findViewById(R.id.sp_gzlx);
		et_ywlx = (EditText) findViewById(R.id.et_ywlx);
		sp_wcbl = (Spinner) findViewById(R.id.sp_wcbl);
		bt_ywlx = (Button) findViewById(R.id.bt_ywlx);
		bt_ywlx.setOnClickListener(this);
		bt_xmxz = (Button) findViewById(R.id.bt_xmmc);
		bt_xmxz.setOnClickListener(this);
		et_xmmc = (EditText) findViewById(R.id.et_xmmc);
		et_ywsj = (EditText) findViewById(R.id.et_ywsj);
		bt_tjbc = (Button) findViewById(R.id.bt_tjbc);
		bt_tjbc.setOnClickListener(this);
		bt_rwgl = (Button) findViewById(R.id.bt_rwgl);
		bt_rwgl.setOnClickListener(this);
		et_rwmc = (EditText) findViewById(R.id.et_rwmc);
		et_jbsj = (EditText) findViewById(R.id.et_jbsj);
		et_ywlr = (EditText) findViewById(R.id.et_ywlr);
		cbIsBm = (CheckBox) findViewById(R.id.cbIsBm);
		cbIsBm.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					et_ywlr.setText("保密");
				}
				else{
					et_ywlr.setText("");
				}
			}
		});

		if(InitAddWorkContentPageInfoTask.getWorkTypeCodeItemsCount(this,"日志明细工作类别")==0){
			top_pb.setVisibility(View.VISIBLE);
			Task task = new InitAddWorkContentPageInfoTask(this, null);
			MainService.addTask(task);
		}
		else{
			initSP();
		}
		gzrzguid = getIntent().getStringExtra("rzguid");
		rzdate = getIntent().getStringExtra("rzdate");

		changeTvColor();
	}
	
	private void changeTvColor(){
		TextView tvGzlx = (TextView) findViewById(R.id.tv_gzlx);
		setTextViewFontColor(tvGzlx);
		TextView tvYwlx = (TextView) findViewById(R.id.tv_ywlx);
		setTextViewFontColor(tvYwlx);
		TextView tvXmmc = (TextView) findViewById(R.id.tv_xmmc);
		setTextViewFontColor(tvXmmc);
		TextView tvRwmc = (TextView) findViewById(R.id.tv_rwmc);
		setTextViewFontColor(tvRwmc);
		TextView tvYwsj = (TextView) findViewById(R.id.tv_ywsj);
		setTextViewFontColor(tvYwsj);
		TextView tvYwsj_xs = (TextView) findViewById(R.id.tv_ywsjxs);
		setTextViewFontColor(tvYwsj_xs);
		TextView tvWcbl = (TextView) findViewById(R.id.tv_wcbl);
		setTextViewFontColor(tvWcbl);
		TextView tvYwlr = (TextView) findViewById(R.id.tv_ywlr);
		setTextViewFontColor(tvYwlr);
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			if(taskid==1){

				initSP();

			}
			else if(taskid==2){
				String bs = param[1].toString();
				if(bs.contains("<result>true</result>")){
					ToastHelp.showToast(this, "工作内容保存成功！");
					gzrzguid = StringHelp.getXMLAtt(bs,"rzguid");
					finish();
					DBHelp.setConfigValue(this, ConfigKey.AddWorkContent_BusinessType, et_ywlx.getText().toString());
					DBHelp.setConfigValue(this, ConfigKey.AddWorkContent_WorkTypeIndex, String.valueOf(sp_gzlx.getSelectedItemPosition()));
				}
				else{
					AlertHelp.ShowAlert(this, "保存失败！");
					bt_tjbc.setEnabled(true);
				}
			}
		}
		top_pb.setVisibility(View.INVISIBLE);
		if(m_Dialog!=null){
			m_Dialog.dismiss();	
		}
	}

	public void initSP(){

		gzlxlist = InitAddWorkContentPageInfoTask.getCodeItem_GZLX(this);
		ArrayAdapter<String> worktypeadapter = ControlsHelp.getSPAdapter(this);
		for(CodeItemsModel cim:gzlxlist){
			worktypeadapter.add(cim.ItemText);
		}
		sp_gzlx.setAdapter(worktypeadapter);
		sp_gzlx.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				et_ywlx.setText("");
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

		ArrayAdapter<String> compeletepercentadapter = ControlsHelp.getSPAdapter(this);
		for(String s:compeletepercentS){
			compeletepercentadapter.add(s);
		}
		sp_wcbl.setAdapter(compeletepercentadapter);

		String saved_businessType = DBHelp.getConfigValue(this, ConfigKey.AddWorkContent_BusinessType);
		String saved_worktypeIndex = DBHelp.getConfigValue(this, ConfigKey.AddWorkContent_WorkTypeIndex);
		if(!saved_businessType.equals("")){
			et_ywlx.setText(saved_businessType);
		}
		if(!saved_worktypeIndex.equals("")){
			sp_gzlx.setSelection(Integer.parseInt(saved_worktypeIndex));
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.addnewworkcontentview;
	}

	@Override
	public String setTitle() {
		return "新增工作内容";
	}

	@Override
	public void onClick(View v) {
		if(v==bt_ywlx){
			resumetype = 1;
			String rootitemvalue = gzlxlist.get(sp_gzlx.getSelectedItemPosition()).ItemValue;
			new NewTree(this, "业务类型选择", "日志明细工作类别", 2,rootitemvalue,null,false,1);
		}
		else if(v==bt_xmxz){
			resumetype = 2;
			Intent it = new Intent(this,XMChoose.class);
			startActivity(it);
		}
		else if(v==bt_tjbc){
			Double ywsj = Double.parseDouble(et_ywsj.getText().toString().trim().equals("")?"0":et_ywsj.getText().toString().trim());
			Double jbsj = Double.parseDouble(et_jbsj.getText().toString().trim().equals("")?"0":et_jbsj.getText().toString().trim());
			
			if(ControlsHelp.Check_EditText_Blank_Warning(et_ywlx, "请选择业务类型"))
			{
				return;
			}
			
			if(ywsj<=0){
				AlertHelp.ShowAlert2(this, "工作时间必须大于0小时");
				return;
			}

			showDialog_wait(null);
			taskid = 2;
			bt_tjbc.setEnabled(false);
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("worktype", sp_gzlx.getSelectedItem());
			param.put("businesstype", et_ywlx.getText());
			param.put("projectname", et_xmmc.getText());
			param.put("projectguid", et_xmmc.getTag());
			param.put("missionname", et_rwmc.getText());
			param.put("missionguid", et_rwmc.getTag());
			param.put("ywtime", et_ywsj.getText());
			param.put("jbtime", et_jbsj.getText());
			param.put("completepercent", sp_wcbl.getSelectedItem());
			param.put("businesscontent", et_ywlr.getText());
			param.put("rzguid", gzrzguid);
			param.put("rzdate", rzdate);
			param.put("isbm", cbIsBm.isChecked()?"1":"0");
			Task task = new AddNewWorkContentTask(this, param);
			MainService.addTask(task);

		}
		else if(v==bt_rwgl){
			resumetype = 3;
			Intent it = new Intent(this,AddNewWorkContentViewMission.class);
			startActivity(it);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		if(resumetype==1){
			if(Tree.chooseNodeData!=null){
				if(Tree.chooseNodeData.size()>0){
					cim_ywlx.ItemText = Tree.chooseNodeData.get(0).get("text");
					cim_ywlx.ItemValue =  Tree.chooseNodeData.get(0).get("param1");
					cim_ywlx.CodeName = Tree.chooseNodeData.get(0).get("param2");
					et_ywlx.setText(cim_ywlx.ItemText);
				}
			}
		}
		else if(resumetype==2){
			if(XMChoose.choosedProject!=null){
				pim_xmmc = XMChoose.choosedProject;
				et_xmmc.setText(pim_xmmc.XiangMuMC);
				et_xmmc.setTag(pim_xmmc.rowguid);
			}
		}
		else if(resumetype==3){
			if(AddNewWorkContentViewMission.choose_mim!=null){
				mim_rwgl = AddNewWorkContentViewMission.choose_mim;
				String percent = switchPercent(mim_rwgl.CompletePercent);
				sp_wcbl.setSelection(Integer.parseInt(percent));
				et_xmmc.setText(mim_rwgl.ProjectName);
				et_xmmc.setTag(mim_rwgl.ProjectGuid);
				et_rwmc.setText(mim_rwgl.MissionName);
				et_rwmc.setTag(mim_rwgl.RowGuid);
			}
		}
	
	}

	private String switchPercent(String s){
		if(s.length()==1){

		}
		else if(s.length()==2){
			s = s.substring(0,1);
		}
		else if(s.length()==3){
			s = s.substring(0,2);
		}
		return s;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Tree.chooseNodeData.clear();
	}


	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}

}
